<template>
  <div>
    <el-form
      :model="subjectForm"
      class="forming"
      :rules="rules"
      ref="subjectForm"
    >
      <el-form-item label="所属科目" width="400px" prop="label" class="box">
        <el-select
          v-model="subjectForm.subjectID"
          placeholder="请选择"
          class="inp"
        >
          <el-option
            v-for="item in subjectList"
            :label="item.label"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" width="280px" prop="directoryName">
        <el-input
          class="inp"
          v-model="subjectForm.directoryName"
          autocomplete="off"
          placeholder="请输入目录名称"
          :maxlength="5"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('onclose')">取 消</el-button>
      <el-button type="primary" @click="addSubject('subjectForm')"
        >确 定</el-button
      >
    </div>
  </div>
</template>

<script>
// 添加
import { add } from '@/api/hmmm/directorys'

export default {
  name: 'DirectorysAdd',
  components: {},
  directives: {},

  props: {
    subjectList: {}
  },
  data() {
    return {
      labelPosition: 'left',
      subjectForm: {
        subjectID: null, // 学科ID
        directoryName: null // 目录名称
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  // created() {
  //   console.log(this.subjectList)
  // },
  methods: {
    addSubject(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          add(this.subjectForm)
            .then(data => {
              console.log(data)
              this.$message.success('新增学科成功！')
            })
            .catch(() => {
              this.$message.error('新增学科失败！')
            })
          this.$emit('onclose')
          this.$emit('update-list')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.inp {
  width: 280px;
}
.box {
  padding-left: 8px;
}
.el-form-item__content .inp .el-form-item__error {
  padding-left: 135px;
  padding-top: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}
::v-deep .el-form-item__error {
  position: absolute;
  top: 100%;
  left: 80px !important;
  z-index: 999;
}
</style>
